<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Command-module for moving and controlling windows &mdash; Dragonfly-modules v0.1 documentation</title>
    <link rel="stylesheet" href="_static/default.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '',
        VERSION:     '0.1',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="top" title="Dragonfly-modules v0.1 documentation" href="index.html" />
    <link rel="next" title="Command-module for cursor movement and editing" href="mod-_multiedit.html" />
    <link rel="prev" title="Command-module for managing Dragonfly" href="mod-_dragonfly_tools.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="mod-_multiedit.html" title="Command-module for cursor movement and editing"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="mod-_dragonfly_tools.html" title="Command-module for managing Dragonfly"
             accesskey="P">previous</a> |</li>
        <li><a href="index.html">Dragonfly-modules v0.1 documentation</a> &raquo;</li> 
      </ul>
    </div>  
    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  
  <div class="section" id="command-module-for-moving-and-controlling-windows">
<h1>Command-module for moving and controlling <strong>windows</strong><a class="headerlink" href="#command-module-for-moving-and-controlling-windows" title="Permalink to this headline">¶</a></h1>
<p>This command-module offers commands for naming windows, bringing
named windows to the foreground, and positioning and resizing
windows.</p>
<div class="section" id="commands">
<h2>Commands<a class="headerlink" href="#commands" title="Permalink to this headline">¶</a></h2>
<p>The following voice commands are available:</p>
<dl class="docutils">
<dt>Command: <strong>&#8220;name window &lt;dictation&gt;&#8221;</strong></dt>
<dd>Assigns the given name to the current foreground window.</dd>
<dt>Command: <strong>&#8220;focus &lt;window name&gt;&#8221;</strong> or <strong>&#8220;bring &lt;window name&gt; to the foreground&#8221;</strong></dt>
<dd>Brings the named window to the foreground.</dd>
<dt>Command: <strong>&#8220;focus title &lt;window title&gt;&#8221;</strong></dt>
<dd>Brings a window with the given word(s) in the title to the foreground.</dd>
<dt>Command: <strong>&#8220;place &lt;window&gt; &lt;position&gt; [on &lt;monitor&gt;]&#8221;</strong></dt>
<dd>Relocates the target window to the given position.</dd>
<dt>Command: <strong>&#8220;stretch &lt;window&gt; &lt;position&gt;&#8221;</strong></dt>
<dd>Stretches the target window to the given position.</dd>
</dl>
</div>
<div class="section" id="usage-examples">
<h2>Usage examples<a class="headerlink" href="#usage-examples" title="Permalink to this headline">¶</a></h2>
<blockquote>
<ul class="simple">
<li>Say <strong>&#8220;place window left&#8221;</strong> to relocate the foreground window
to the left side of the monitor it&#8217;s on.</li>
<li>Say <strong>&#8220;place Firefox top right on monitor 2&#8221;</strong> to relocate
the window which was previously named &#8220;Firefox&#8221; to the top right
corner of the second display monitor.</li>
</ul>
</blockquote>
</div>
<div class="section" id="configuration-examples">
<h2>Configuration examples<a class="headerlink" href="#configuration-examples" title="Permalink to this headline">¶</a></h2>
<div class="section" id="configuration-winctrl-en-txt">
<h3>Configuration &#8220;_winctrl-en.txt&#8221;<a class="headerlink" href="#configuration-winctrl-en-txt" title="Permalink to this headline">¶</a></h3>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><pre> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65</pre></td><td class="code"><div class="highlight"><pre><span class="c">#</span>
<span class="c"># Dragonfly config for Window control</span>
<span class="c">#</span>

<span class="c">#--- Language section ------------------------------------------------------</span>

<span class="n">lang</span><span class="o">.</span><span class="n">name_win</span> <span class="o">=</span> <span class="s">&#39;name (window | win) &lt;name&gt;&#39;</span>
                    <span class="c"># Command to give the foreground window a name;</span>
                    <span class="c">#  must contain the &lt;name&gt; extra.</span>
                    <span class="c"># Default: &#39;name (window | win) &lt;name&gt;&#39;</span>

<span class="n">lang</span><span class="o">.</span><span class="n">focus_win</span> <span class="o">=</span> <span class="s">&#39;focus &lt;win_names&gt; | bring &lt;win_names&gt; to [the] (top | foreground)&#39;</span>
                    <span class="c"># Command to bring a named window to the</span>
                    <span class="c">#  foreground.</span>
                    <span class="c"># Default: &#39;focus &lt;win_names&gt; | bring &lt;win_names&gt;</span>
                    <span class="c">#  to [the] (top | foreground)&#39;</span>

<span class="n">lang</span><span class="o">.</span><span class="n">translate_win</span> <span class="o">=</span> <span class="s">&#39;place &lt;win_selector&gt; &lt;position&gt; [on &lt;mon_selector&gt;]&#39;</span>
                    <span class="c"># Command to translate a window.</span>
                    <span class="c"># Default: &#39;place &lt;win_selector&gt; &lt;position&gt; [on</span>
                    <span class="c">#  &lt;mon_selector&gt;]&#39;</span>

<span class="n">lang</span><span class="o">.</span><span class="n">resize_win</span> <span class="o">=</span> <span class="s">&#39;place &lt;win_selector&gt; [from] &lt;position&gt; [to] &lt;position&gt; [on &lt;mon_selector&gt;]&#39;</span>
                    <span class="c"># Command to move and resize a window.</span>
                    <span class="c"># Default: &#39;place &lt;win_selector&gt; [from] &lt;position&gt;</span>
                    <span class="c">#  [to] &lt;position&gt; [on &lt;mon_selector&gt;]&#39;</span>

<span class="n">lang</span><span class="o">.</span><span class="n">stretch_win</span> <span class="o">=</span> <span class="s">&#39;stretch &lt;win_selector&gt; [to] &lt;position&gt;&#39;</span>
                    <span class="c"># Command to stretch a window.</span>
                    <span class="c"># Default: &#39;stretch &lt;win_selector&gt; [to]</span>
                    <span class="c">#  &lt;position&gt;&#39;</span>

<span class="n">lang</span><span class="o">.</span><span class="n">win_selector</span> <span class="o">=</span> <span class="s">&#39;window | win | [window] &lt;win_names&gt;&#39;</span>
                    <span class="c"># Partial command for specifying a window; must</span>
                    <span class="c">#  contain the &lt;win_names&gt; extra.</span>
                    <span class="c"># Default: &#39;window | win | [window] &lt;win_names&gt;&#39;</span>

<span class="n">lang</span><span class="o">.</span><span class="n">mon_selector</span> <span class="o">=</span> <span class="s">&#39;(this | current) monitor | [monitor] &lt;mon_names&gt;&#39;</span>
                    <span class="c"># Partial command for specifying a monitor; must</span>
                    <span class="c">#  contain the &lt;mon_names&gt; extra.</span>
                    <span class="c"># Default: &#39;(this | current) monitor | [monitor]</span>
                    <span class="c">#  &lt;mon_names&gt;&#39;</span>

<span class="n">lang</span><span class="o">.</span><span class="n">left</span> <span class="o">=</span> <span class="s">&#39;left&#39;</span>
                    <span class="c"># Word for left side of monitor.</span>
                    <span class="c"># Default: &#39;left&#39;</span>

<span class="n">lang</span><span class="o">.</span><span class="n">right</span> <span class="o">=</span> <span class="s">&#39;right&#39;</span>
                    <span class="c"># Word for right side of monitor.</span>
                    <span class="c"># Default: &#39;right&#39;</span>

<span class="n">lang</span><span class="o">.</span><span class="n">top</span> <span class="o">=</span> <span class="s">&#39;top&#39;</span>
                    <span class="c"># Word for top side of monitor.</span>
                    <span class="c"># Default: &#39;top&#39;</span>

<span class="n">lang</span><span class="o">.</span><span class="n">bottom</span> <span class="o">=</span> <span class="s">&#39;bottom&#39;</span>
                    <span class="c"># Word for bottom side of monitor.</span>
                    <span class="c"># Default: &#39;bottom&#39;</span>

<span class="c">#--- Settings section ------------------------------------------------------</span>

<span class="n">settings</span><span class="o">.</span><span class="n">grid</span> <span class="o">=</span> <span class="mf">10</span>
                    <span class="c"># The number of grid divisions a monitor is</span>
                    <span class="c">#  divided up into when placing windows.</span>
                    <span class="c"># Default: 10</span>
</pre></div>
</td></tr></table></div>
</div>
</div>
<div class="section" id="module-source-code">
<h2>Module source code<a class="headerlink" href="#module-source-code" title="Permalink to this headline">¶</a></h2>
<p>The most current version of this module can be downloaded from the <a class="reference external" href="http://dragonfly-modules.googlecode.com/svn/trunk/command-modules/_winctrl.py">repository here</a>.</p>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416</pre></td><td class="code"><div class="highlight"><pre><span class="c">#</span>
<span class="c"># This file is a command-module for Dragonfly.</span>
<span class="c"># (c) Copyright 2008 by Christo Butcher</span>
<span class="c"># Licensed under the LGPL, see &lt;http://www.gnu.org/licenses/&gt;</span>
<span class="c">#</span>

<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Command-module for moving and controlling **windows**</span>
<span class="sd">=====================================================</span>

<span class="sd">This command-module offers commands for naming windows, bringing</span>
<span class="sd">named windows to the foreground, and positioning and resizing</span>
<span class="sd">windows.</span>

<span class="sd">Commands</span>
<span class="sd">--------</span>

<span class="sd">The following voice commands are available:</span>

<span class="sd">Command: **&quot;name window &lt;dictation&gt;&quot;**</span>
<span class="sd">    Assigns the given name to the current foreground window.</span>

<span class="sd">Command: **&quot;focus &lt;window name&gt;&quot;** or **&quot;bring &lt;window name&gt; to the foreground&quot;**</span>
<span class="sd">    Brings the named window to the foreground.</span>

<span class="sd">Command: **&quot;focus title &lt;window title&gt;&quot;**</span>
<span class="sd">    Brings a window with the given word(s) in the title to the foreground.</span>

<span class="sd">Command: **&quot;place &lt;window&gt; &lt;position&gt; [on &lt;monitor&gt;]&quot;**</span>
<span class="sd">    Relocates the target window to the given position.</span>

<span class="sd">Command: **&quot;stretch &lt;window&gt; &lt;position&gt;&quot;**</span>
<span class="sd">    Stretches the target window to the given position.</span>

<span class="sd">Usage examples</span>
<span class="sd">--------------</span>

<span class="sd"> - Say **&quot;place window left&quot;** to relocate the foreground window</span>
<span class="sd">   to the left side of the monitor it&#39;s on.</span>
<span class="sd"> - Say **&quot;place Firefox top right on monitor 2&quot;** to relocate</span>
<span class="sd">   the window which was previously named &quot;Firefox&quot; to the top right</span>
<span class="sd">   corner of the second display monitor.</span>

<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">import</span> <span class="nn">pkg_resources</span>
<span class="n">pkg_resources</span><span class="o">.</span><span class="n">require</span><span class="p">(</span><span class="s">&quot;dragonfly &gt;= 0.6.5beta1.dev-r76&quot;</span><span class="p">)</span>

<span class="kn">import</span> <span class="nn">time</span>
<span class="kn">from</span> <span class="nn">dragonfly</span> <span class="kn">import</span> <span class="p">(</span><span class="n">Grammar</span><span class="p">,</span> <span class="n">Alternative</span><span class="p">,</span> <span class="n">RuleRef</span><span class="p">,</span> <span class="n">DictListRef</span><span class="p">,</span>
                       <span class="n">Dictation</span><span class="p">,</span> <span class="n">Compound</span><span class="p">,</span> <span class="n">Integer</span><span class="p">,</span> <span class="n">Rule</span><span class="p">,</span> <span class="n">CompoundRule</span><span class="p">,</span>
                       <span class="n">DictList</span><span class="p">,</span> <span class="n">Window</span><span class="p">,</span> <span class="n">Rectangle</span><span class="p">,</span> <span class="n">monitors</span><span class="p">,</span>
                       <span class="n">Config</span><span class="p">,</span> <span class="n">Section</span><span class="p">,</span> <span class="n">Item</span><span class="p">,</span> <span class="n">FocusWindow</span><span class="p">,</span> <span class="n">ActionError</span><span class="p">)</span>


<span class="c">#---------------------------------------------------------------------------</span>
<span class="c"># Set up this module&#39;s configuration.</span>

<span class="n">config</span> <span class="o">=</span> <span class="n">Config</span><span class="p">(</span><span class="s">&quot;Window control&quot;</span><span class="p">)</span>
<span class="n">config</span><span class="o">.</span><span class="n">lang</span>                <span class="o">=</span> <span class="n">Section</span><span class="p">(</span><span class="s">&quot;Language section&quot;</span><span class="p">)</span>
<span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">name_win</span>       <span class="o">=</span> <span class="n">Item</span><span class="p">(</span><span class="s">&quot;name (window | win) &lt;name&gt;&quot;</span><span class="p">,</span>
                                  <span class="n">doc</span><span class="o">=</span><span class="s">&quot;Command to give the foreground window a name; must contain the &lt;name&gt; extra.&quot;</span><span class="p">)</span>
<span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">focus_win</span>      <span class="o">=</span> <span class="n">Item</span><span class="p">(</span><span class="s">&quot;focus &lt;win_selector&gt; | bring &lt;win_selector&gt; to [the] (top | foreground)&quot;</span><span class="p">,</span>
                                  <span class="n">doc</span><span class="o">=</span><span class="s">&quot;Command to bring a named window to the foreground.&quot;</span><span class="p">)</span>
<span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">focus_title</span>    <span class="o">=</span> <span class="n">Item</span><span class="p">(</span><span class="s">&quot;focus title &lt;text&gt;&quot;</span><span class="p">,</span>
                                  <span class="n">doc</span><span class="o">=</span><span class="s">&quot;Command to bring a window with the given title to the foreground.&quot;</span><span class="p">)</span>
<span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">translate_win</span>  <span class="o">=</span> <span class="n">Item</span><span class="p">(</span><span class="s">&quot;place &lt;win_selector&gt; &lt;position&gt; [on &lt;mon_selector&gt;]&quot;</span><span class="p">,</span>
                                  <span class="n">doc</span><span class="o">=</span><span class="s">&quot;Command to translate a window.&quot;</span><span class="p">)</span>
<span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">resize_win</span>     <span class="o">=</span> <span class="n">Item</span><span class="p">(</span><span class="s">&quot;place &lt;win_selector&gt; [from] &lt;position&gt; [to] &lt;position&gt; [on &lt;mon_selector&gt;]&quot;</span><span class="p">,</span>
                                  <span class="n">doc</span><span class="o">=</span><span class="s">&quot;Command to move and resize a window.&quot;</span><span class="p">)</span>
<span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">stretch_win</span>    <span class="o">=</span> <span class="n">Item</span><span class="p">(</span><span class="s">&quot;stretch &lt;win_selector&gt; [to] &lt;position&gt;&quot;</span><span class="p">,</span>
                                  <span class="n">doc</span><span class="o">=</span><span class="s">&quot;Command to stretch a window.&quot;</span><span class="p">)</span>
<span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">win_selector</span>   <span class="o">=</span> <span class="n">Item</span><span class="p">(</span><span class="s">&quot;window | win | [window] &lt;win_names&gt;&quot;</span><span class="p">,</span>
                                  <span class="n">doc</span><span class="o">=</span><span class="s">&quot;Partial command for specifying a window; must contain the &lt;win_names&gt; extra.&quot;</span><span class="p">)</span>
<span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">mon_selector</span>   <span class="o">=</span> <span class="n">Item</span><span class="p">(</span><span class="s">&quot;(this | current) monitor | [monitor] &lt;mon_names&gt;&quot;</span><span class="p">,</span>
                                  <span class="n">doc</span><span class="o">=</span><span class="s">&quot;Partial command for specifying a monitor; must contain the &lt;mon_names&gt; extra.&quot;</span><span class="p">)</span>
<span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">left</span>           <span class="o">=</span> <span class="n">Item</span><span class="p">(</span><span class="s">&quot;left&quot;</span><span class="p">,</span> <span class="n">doc</span><span class="o">=</span><span class="s">&quot;Word for left side of monitor.&quot;</span><span class="p">)</span>
<span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">right</span>          <span class="o">=</span> <span class="n">Item</span><span class="p">(</span><span class="s">&quot;right&quot;</span><span class="p">,</span> <span class="n">doc</span><span class="o">=</span><span class="s">&quot;Word for right side of monitor.&quot;</span><span class="p">)</span>
<span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">top</span>            <span class="o">=</span> <span class="n">Item</span><span class="p">(</span><span class="s">&quot;top&quot;</span><span class="p">,</span> <span class="n">doc</span><span class="o">=</span><span class="s">&quot;Word for top side of monitor.&quot;</span><span class="p">)</span>
<span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">bottom</span>         <span class="o">=</span> <span class="n">Item</span><span class="p">(</span><span class="s">&quot;bottom&quot;</span><span class="p">,</span> <span class="n">doc</span><span class="o">=</span><span class="s">&quot;Word for bottom side of monitor.&quot;</span><span class="p">)</span>
<span class="n">config</span><span class="o">.</span><span class="n">settings</span>            <span class="o">=</span> <span class="n">Section</span><span class="p">(</span><span class="s">&quot;Settings section&quot;</span><span class="p">)</span>
<span class="n">config</span><span class="o">.</span><span class="n">settings</span><span class="o">.</span><span class="n">grid</span>       <span class="o">=</span> <span class="n">Item</span><span class="p">(</span><span class="mf">10</span><span class="p">,</span> <span class="n">doc</span><span class="o">=</span><span class="s">&quot;The number of grid divisions a monitor is divided up into when placing windows.&quot;</span><span class="p">)</span>
<span class="n">config</span><span class="o">.</span><span class="n">settings</span><span class="o">.</span><span class="n">defaults</span>   <span class="o">=</span> <span class="n">Item</span><span class="p">({</span><span class="s">&quot;fire&quot;</span><span class="p">:</span> <span class="p">(</span><span class="s">&quot;firefox&quot;</span><span class="p">,</span> <span class="bp">None</span><span class="p">)},</span> <span class="n">doc</span><span class="o">=</span><span class="s">&quot;Default window names.  Maps spoken-forms to (executable, title) pairs.&quot;</span><span class="p">)</span>
<span class="c">#config.generate_config_file()</span>
<span class="n">config</span><span class="o">.</span><span class="n">load</span><span class="p">()</span>


<span class="c">#===========================================================================</span>
<span class="c"># Create this module&#39;s main grammar object.</span>

<span class="n">grammar</span> <span class="o">=</span> <span class="n">Grammar</span><span class="p">(</span><span class="s">&quot;window control&quot;</span><span class="p">)</span>


<span class="c">#---------------------------------------------------------------------------</span>
<span class="c"># Dictionary list of window and monitor names.</span>

<span class="n">win_names</span>     <span class="o">=</span> <span class="n">DictList</span><span class="p">(</span><span class="s">&quot;win_names&quot;</span><span class="p">)</span>
<span class="n">win_names_ref</span> <span class="o">=</span> <span class="n">DictListRef</span><span class="p">(</span><span class="s">&quot;win_names&quot;</span><span class="p">,</span> <span class="n">win_names</span><span class="p">)</span>
<span class="n">mon_names</span>     <span class="o">=</span> <span class="n">DictList</span><span class="p">(</span><span class="s">&quot;mon_names&quot;</span><span class="p">)</span>
<span class="n">mon_names_ref</span> <span class="o">=</span> <span class="n">DictListRef</span><span class="p">(</span><span class="s">&quot;mon_names&quot;</span><span class="p">,</span> <span class="n">mon_names</span><span class="p">)</span>

<span class="c"># Populate monitor names.</span>
<span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">m</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">monitors</span><span class="p">):</span>
    <span class="n">mon_names</span><span class="p">[</span><span class="nb">str</span><span class="p">(</span><span class="n">i</span><span class="o">+</span><span class="mf">1</span><span class="p">)]</span> <span class="o">=</span> <span class="n">m</span>


<span class="c">#---------------------------------------------------------------------------</span>
<span class="c"># Default window names handling.</span>

<span class="n">default_names</span> <span class="o">=</span> <span class="n">config</span><span class="o">.</span><span class="n">settings</span><span class="o">.</span><span class="n">defaults</span>

<span class="c"># Pre-populate the win_names mapping with the given default names.</span>
<span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">default_names</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
    <span class="n">win_names</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">key</span>

<span class="c"># Helper function to search for a default-name window.</span>
<span class="k">def</span> <span class="nf">get_default_window</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
    <span class="n">executable</span><span class="p">,</span> <span class="n">title</span> <span class="o">=</span> <span class="n">default_names</span><span class="p">[</span><span class="n">name</span><span class="p">]</span>
    <span class="k">if</span> <span class="n">executable</span><span class="p">:</span> <span class="n">executable</span> <span class="o">=</span> <span class="n">executable</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
    <span class="k">if</span> <span class="n">title</span><span class="p">:</span> <span class="n">title</span> <span class="o">=</span> <span class="n">title</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
    <span class="n">windows</span> <span class="o">=</span> <span class="n">Window</span><span class="o">.</span><span class="n">get_all_windows</span><span class="p">()</span>
    <span class="k">for</span> <span class="n">window</span> <span class="ow">in</span> <span class="n">windows</span><span class="p">:</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">window</span><span class="o">.</span><span class="n">is_visible</span><span class="p">:</span>
            <span class="k">continue</span>
        <span class="k">elif</span> <span class="n">executable</span> <span class="ow">and</span> <span class="n">window</span><span class="o">.</span><span class="n">executable</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">executable</span><span class="p">)</span> <span class="o">==</span> <span class="o">-</span><span class="mf">1</span><span class="p">:</span>
            <span class="k">continue</span>
        <span class="k">elif</span> <span class="n">title</span> <span class="ow">and</span> <span class="n">window</span><span class="o">.</span><span class="n">title</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span><span class="o">.</span><span class="n">find</span><span class="p">(</span><span class="n">title</span><span class="p">)</span> <span class="o">==</span> <span class="o">-</span><span class="mf">1</span><span class="p">:</span>
            <span class="k">continue</span>
        <span class="n">window</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">name</span>
        <span class="n">win_names</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">window</span>
        <span class="k">return</span> <span class="n">window</span>
    <span class="k">return</span> <span class="bp">None</span>


<span class="c">#---------------------------------------------------------------------------</span>
<span class="c"># Internal window selector rule and element.</span>

<span class="k">class</span> <span class="nc">WinSelectorRule</span><span class="p">(</span><span class="n">CompoundRule</span><span class="p">):</span>

    <span class="n">spec</span> <span class="o">=</span> <span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">win_selector</span>
    <span class="n">extras</span> <span class="o">=</span> <span class="p">[</span><span class="n">win_names_ref</span><span class="p">]</span>
    <span class="n">exported</span> <span class="o">=</span> <span class="bp">False</span>

    <span class="k">def</span> <span class="nf">value</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">node</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">node</span><span class="o">.</span><span class="n">has_child_with_name</span><span class="p">(</span><span class="s">&quot;win_names&quot;</span><span class="p">):</span>
            <span class="n">window</span> <span class="o">=</span> <span class="n">node</span><span class="o">.</span><span class="n">get_child_by_name</span><span class="p">(</span><span class="s">&quot;win_names&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">value</span><span class="p">()</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">window</span><span class="p">,</span> <span class="n">Window</span><span class="p">):</span>
                <span class="n">window</span> <span class="o">=</span> <span class="n">get_default_window</span><span class="p">(</span><span class="n">window</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">window</span>
        <span class="k">return</span> <span class="n">Window</span><span class="o">.</span><span class="n">get_foreground</span><span class="p">()</span>

<span class="n">win_selector</span> <span class="o">=</span> <span class="n">RuleRef</span><span class="p">(</span><span class="n">WinSelectorRule</span><span class="p">(),</span> <span class="n">name</span><span class="o">=</span><span class="s">&quot;win_selector&quot;</span><span class="p">)</span>


<span class="c">#---------------------------------------------------------------------------</span>
<span class="c"># Internal monitor selector rule and element.</span>

<span class="k">class</span> <span class="nc">MonSelectorRule</span><span class="p">(</span><span class="n">CompoundRule</span><span class="p">):</span>

    <span class="n">spec</span> <span class="o">=</span> <span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">mon_selector</span>
    <span class="n">extras</span> <span class="o">=</span> <span class="p">[</span><span class="n">mon_names_ref</span><span class="p">]</span>
    <span class="n">exported</span> <span class="o">=</span> <span class="bp">False</span>

    <span class="k">def</span> <span class="nf">value</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">node</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">node</span><span class="o">.</span><span class="n">has_child_with_name</span><span class="p">(</span><span class="s">&quot;mon_names&quot;</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">node</span><span class="o">.</span><span class="n">get_child_by_name</span><span class="p">(</span><span class="s">&quot;mon_names&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">value</span><span class="p">()</span>
        <span class="k">return</span> <span class="bp">None</span>

<span class="n">mon_selector</span> <span class="o">=</span> <span class="n">RuleRef</span><span class="p">(</span><span class="n">MonSelectorRule</span><span class="p">(),</span> <span class="n">name</span><span class="o">=</span><span class="s">&quot;mon_selector&quot;</span><span class="p">)</span>


<span class="c">#---------------------------------------------------------------------------</span>
<span class="c"># Exported window naming rule.</span>

<span class="k">class</span> <span class="nc">NameWinRule</span><span class="p">(</span><span class="n">CompoundRule</span><span class="p">):</span>

    <span class="n">spec</span> <span class="o">=</span> <span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">name_win</span>
    <span class="n">extras</span> <span class="o">=</span> <span class="p">[</span><span class="n">Dictation</span><span class="p">(</span><span class="s">&quot;name&quot;</span><span class="p">)]</span>

    <span class="k">def</span> <span class="nf">_process_recognition</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">node</span><span class="p">,</span> <span class="n">extras</span><span class="p">):</span>
        <span class="n">name</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">extras</span><span class="p">[</span><span class="s">&quot;name&quot;</span><span class="p">])</span>
        <span class="n">window</span> <span class="o">=</span> <span class="n">Window</span><span class="o">.</span><span class="n">get_foreground</span><span class="p">()</span>
        <span class="n">window</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">name</span>
        <span class="n">win_names</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">window</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%s</span><span class="s">: named foreground window &#39;</span><span class="si">%s</span><span class="s">&#39;.&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">window</span><span class="p">))</span>

<span class="n">grammar</span><span class="o">.</span><span class="n">add_rule</span><span class="p">(</span><span class="n">NameWinRule</span><span class="p">())</span>


<span class="c">#---------------------------------------------------------------------------</span>
<span class="c"># Exported window focusing rule; brings named windows to the foreground.</span>

<span class="k">class</span> <span class="nc">FocusWinRule</span><span class="p">(</span><span class="n">CompoundRule</span><span class="p">):</span>

    <span class="n">spec</span> <span class="o">=</span> <span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">focus_win</span>
    <span class="n">extras</span> <span class="o">=</span> <span class="p">[</span><span class="n">win_selector</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">_process_recognition</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">node</span><span class="p">,</span> <span class="n">extras</span><span class="p">):</span>
        <span class="n">window</span> <span class="o">=</span> <span class="n">extras</span><span class="p">[</span><span class="s">&quot;win_selector&quot;</span><span class="p">]</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">window</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_log</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s">&quot;No window with that name found.&quot;</span><span class="p">)</span>
            <span class="k">return</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%s</span><span class="s">: bringing window &#39;</span><span class="si">%s</span><span class="s">&#39; to the foreground.&quot;</span>
                        <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">window</span><span class="p">))</span>
        <span class="k">for</span> <span class="n">attempt</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mf">4</span><span class="p">):</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">window</span><span class="o">.</span><span class="n">set_foreground</span><span class="p">()</span>
            <span class="k">except</span> <span class="ne">Exception</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_log</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%s</span><span class="s">: set_foreground() failed: </span><span class="si">%s</span><span class="s">.&quot;</span>
                                  <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">e</span><span class="p">))</span>
                <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mf">0.2</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">break</span>

<span class="n">grammar</span><span class="o">.</span><span class="n">add_rule</span><span class="p">(</span><span class="n">FocusWinRule</span><span class="p">())</span>


<span class="c">#---------------------------------------------------------------------------</span>
<span class="c"># Exported window focusing rule; brings named windows to the foreground.</span>

<span class="k">class</span> <span class="nc">FocusTitleRule</span><span class="p">(</span><span class="n">CompoundRule</span><span class="p">):</span>

    <span class="n">spec</span> <span class="o">=</span> <span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">focus_title</span>
    <span class="n">extras</span> <span class="o">=</span> <span class="p">[</span><span class="n">Dictation</span><span class="p">(</span><span class="s">&quot;text&quot;</span><span class="p">)]</span>

    <span class="k">def</span> <span class="nf">_process_recognition</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">node</span><span class="p">,</span> <span class="n">extras</span><span class="p">):</span>
        <span class="n">title</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">extras</span><span class="p">[</span><span class="s">&quot;text&quot;</span><span class="p">])</span>
        <span class="n">action</span> <span class="o">=</span> <span class="n">FocusWindow</span><span class="p">(</span><span class="n">title</span><span class="o">=</span><span class="n">title</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">action</span><span class="o">.</span><span class="n">execute</span><span class="p">()</span>
        <span class="k">except</span> <span class="n">ActionError</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_log</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s">&quot;No window with that name found.&quot;</span><span class="p">)</span>

<span class="n">grammar</span><span class="o">.</span><span class="n">add_rule</span><span class="p">(</span><span class="n">FocusTitleRule</span><span class="p">())</span>


<span class="c">#---------------------------------------------------------------------------</span>
<span class="c"># Internal fraction rule.</span>

<span class="k">class</span> <span class="nc">FractionRule</span><span class="p">(</span><span class="n">Rule</span><span class="p">):</span>

    <span class="n">sections</span> <span class="o">=</span> <span class="n">config</span><span class="o">.</span><span class="n">settings</span><span class="o">.</span><span class="n">grid</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">Rule</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span>
                      <span class="n">name</span><span class="o">=</span><span class="s">&quot;fraction&quot;</span><span class="p">,</span>
                      <span class="n">element</span><span class="o">=</span><span class="n">Integer</span><span class="p">(</span><span class="s">&quot;_frac&quot;</span><span class="p">,</span> <span class="mf">0</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">sections</span> <span class="o">+</span> <span class="mf">1</span><span class="p">),</span>
                      <span class="n">exported</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">value</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">node</span><span class="p">):</span>
        <span class="n">value</span> <span class="o">=</span> <span class="n">node</span><span class="o">.</span><span class="n">get_child_by_name</span><span class="p">(</span><span class="s">&quot;_frac&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">value</span><span class="p">()</span>
        <span class="k">return</span> <span class="nb">float</span><span class="p">(</span><span class="n">value</span><span class="p">)</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">sections</span>

<span class="n">fraction_rule</span> <span class="o">=</span> <span class="n">FractionRule</span><span class="p">()</span>


<span class="c">#---------------------------------------------------------------------------</span>

<span class="n">horz_left</span>    <span class="o">=</span> <span class="n">Compound</span><span class="p">(</span><span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">left</span><span class="p">,</span>   <span class="n">name</span><span class="o">=</span><span class="s">&quot;horz&quot;</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mf">0.0</span><span class="p">)</span>
<span class="n">horz_right</span>   <span class="o">=</span> <span class="n">Compound</span><span class="p">(</span><span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">right</span><span class="p">,</span>  <span class="n">name</span><span class="o">=</span><span class="s">&quot;horz&quot;</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mf">1.0</span><span class="p">)</span>
<span class="n">vert_top</span>     <span class="o">=</span> <span class="n">Compound</span><span class="p">(</span><span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">top</span><span class="p">,</span>    <span class="n">name</span><span class="o">=</span><span class="s">&quot;vert&quot;</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mf">0.0</span><span class="p">)</span>
<span class="n">vert_bottom</span>  <span class="o">=</span> <span class="n">Compound</span><span class="p">(</span><span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">bottom</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s">&quot;vert&quot;</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="mf">1.0</span><span class="p">)</span>
<span class="n">horz_frac</span>    <span class="o">=</span> <span class="n">RuleRef</span><span class="p">(</span><span class="n">fraction_rule</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s">&quot;horz&quot;</span><span class="p">)</span>
<span class="n">vert_frac</span>    <span class="o">=</span> <span class="n">RuleRef</span><span class="p">(</span><span class="n">fraction_rule</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s">&quot;vert&quot;</span><span class="p">)</span>

<span class="n">horz_expl</span>    <span class="o">=</span> <span class="n">Alternative</span><span class="p">([</span><span class="n">horz_left</span><span class="p">,</span> <span class="n">horz_right</span><span class="p">],</span>  <span class="n">name</span><span class="o">=</span><span class="s">&quot;horz_expl&quot;</span><span class="p">)</span>
<span class="n">horz_all</span>     <span class="o">=</span> <span class="n">Alternative</span><span class="p">([</span><span class="n">horz_expl</span><span class="p">,</span> <span class="n">horz_frac</span><span class="p">],</span>   <span class="n">name</span><span class="o">=</span><span class="s">&quot;horz_all&quot;</span><span class="p">)</span>
<span class="n">vert_expl</span>    <span class="o">=</span> <span class="n">Alternative</span><span class="p">([</span><span class="n">vert_top</span><span class="p">,</span>  <span class="n">vert_bottom</span><span class="p">],</span> <span class="n">name</span><span class="o">=</span><span class="s">&quot;vert_expl&quot;</span><span class="p">)</span>
<span class="n">vert_all</span>     <span class="o">=</span> <span class="n">Alternative</span><span class="p">([</span><span class="n">vert_expl</span><span class="p">,</span> <span class="n">vert_frac</span><span class="p">],</span>   <span class="n">name</span><span class="o">=</span><span class="s">&quot;vert_all&quot;</span><span class="p">)</span>


<span class="c">#---------------------------------------------------------------------------</span>

<span class="n">position_element</span> <span class="o">=</span> <span class="n">Compound</span><span class="p">(</span>
              <span class="n">spec</span><span class="o">=</span><span class="s">&quot;   &lt;horz_expl&gt;&quot;</span>              <span class="c"># 1D, horizontal</span>
                   <span class="s">&quot; | &lt;vert_expl&gt;&quot;</span>              <span class="c"># 1D, vertical</span>
                   <span class="s">&quot; | &lt;horz_all&gt; &lt;vert_all&gt;&quot;</span>    <span class="c"># 2D, horizontal-vertical</span>
                   <span class="s">&quot; | &lt;vert_expl&gt; &lt;horz_all&gt;&quot;</span>   <span class="c"># 2D, vertical-horizontal</span>
                   <span class="s">&quot; | &lt;vert_all&gt; &lt;horz_expl&gt;&quot;</span><span class="p">,</span>  <span class="c"># 2D, vertical-horizontal</span>
              <span class="n">extras</span><span class="o">=</span><span class="p">[</span><span class="n">horz_expl</span><span class="p">,</span> <span class="n">horz_all</span><span class="p">,</span> <span class="n">vert_expl</span><span class="p">,</span> <span class="n">vert_all</span><span class="p">],</span>
             <span class="p">)</span>
<span class="n">position_rule</span> <span class="o">=</span> <span class="n">Rule</span><span class="p">(</span>
                     <span class="n">name</span><span class="o">=</span><span class="s">&quot;position_rule&quot;</span><span class="p">,</span>
                     <span class="n">element</span><span class="o">=</span><span class="n">position_element</span><span class="p">,</span>
                     <span class="n">exported</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span>
                    <span class="p">)</span>
<span class="n">position</span> <span class="o">=</span> <span class="n">RuleRef</span><span class="p">(</span><span class="n">position_rule</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s">&quot;position&quot;</span><span class="p">)</span>


<span class="c">#---------------------------------------------------------------------------</span>

<span class="k">class</span> <span class="nc">TranslateRule</span><span class="p">(</span><span class="n">CompoundRule</span><span class="p">):</span>

    <span class="n">spec</span> <span class="o">=</span> <span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">translate_win</span>
    <span class="n">extras</span> <span class="o">=</span> <span class="p">[</span>
              <span class="n">win_selector</span><span class="p">,</span>                  <span class="c"># Window selector element</span>
              <span class="n">mon_selector</span><span class="p">,</span>                  <span class="c"># Monitor selector element</span>
              <span class="n">position</span><span class="p">,</span>                      <span class="c"># Position element</span>
             <span class="p">]</span>

    <span class="k">def</span> <span class="nf">_process_recognition</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">node</span><span class="p">,</span> <span class="n">extras</span><span class="p">):</span>
        <span class="c"># Determine which window to place on which monitor.</span>
        <span class="n">window</span> <span class="o">=</span> <span class="n">extras</span><span class="p">[</span><span class="s">&quot;win_selector&quot;</span><span class="p">]</span>
        <span class="k">if</span> <span class="s">&quot;mon_selector&quot;</span> <span class="ow">in</span> <span class="n">extras</span><span class="p">:</span>
            <span class="n">monitor</span> <span class="o">=</span> <span class="n">extras</span><span class="p">[</span><span class="s">&quot;mon_selector&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">rectangle</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">monitor</span> <span class="o">=</span> <span class="n">window</span><span class="o">.</span><span class="n">get_containing_monitor</span><span class="p">()</span><span class="o">.</span><span class="n">rectangle</span>

        <span class="c"># Calculate available area within monitor.</span>
        <span class="n">pos</span> <span class="o">=</span> <span class="n">window</span><span class="o">.</span><span class="n">get_position</span><span class="p">()</span>
        <span class="n">m_x1</span> <span class="o">=</span> <span class="n">monitor</span><span class="o">.</span><span class="n">x1</span> <span class="o">+</span> <span class="n">pos</span><span class="o">.</span><span class="n">dx</span> <span class="o">/</span> <span class="mf">2</span>
        <span class="n">m_dx</span> <span class="o">=</span> <span class="n">monitor</span><span class="o">.</span><span class="n">dx</span> <span class="o">-</span> <span class="n">pos</span><span class="o">.</span><span class="n">dx</span>
        <span class="n">m_y1</span> <span class="o">=</span> <span class="n">monitor</span><span class="o">.</span><span class="n">y1</span> <span class="o">+</span> <span class="n">pos</span><span class="o">.</span><span class="n">dy</span> <span class="o">/</span> <span class="mf">2</span>
        <span class="n">m_dy</span> <span class="o">=</span> <span class="n">monitor</span><span class="o">.</span><span class="n">dy</span> <span class="o">-</span> <span class="n">pos</span><span class="o">.</span><span class="n">dy</span>

        <span class="c"># Get spoken position and calculate how far to move.</span>
        <span class="n">horizontal</span> <span class="o">=</span> <span class="n">node</span><span class="o">.</span><span class="n">get_child_by_name</span><span class="p">(</span><span class="s">&quot;horz&quot;</span><span class="p">)</span>
        <span class="n">vertical</span> <span class="o">=</span> <span class="n">node</span><span class="o">.</span><span class="n">get_child_by_name</span><span class="p">(</span><span class="s">&quot;vert&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">horizontal</span><span class="p">:</span> <span class="n">dx</span> <span class="o">=</span> <span class="n">m_x1</span> <span class="o">+</span> <span class="n">horizontal</span><span class="o">.</span><span class="n">value</span><span class="p">()</span> <span class="o">*</span> <span class="n">m_dx</span> <span class="o">-</span> <span class="n">pos</span><span class="o">.</span><span class="n">center</span><span class="o">.</span><span class="n">x</span>
        <span class="k">else</span><span class="p">:</span>          <span class="n">dx</span> <span class="o">=</span> <span class="mf">0</span>
        <span class="k">if</span> <span class="n">vertical</span><span class="p">:</span>   <span class="n">dy</span> <span class="o">=</span> <span class="n">m_y1</span> <span class="o">+</span> <span class="n">vertical</span><span class="o">.</span><span class="n">value</span><span class="p">()</span> <span class="o">*</span> <span class="n">m_dy</span> <span class="o">-</span> <span class="n">pos</span><span class="o">.</span><span class="n">center</span><span class="o">.</span><span class="n">y</span>
        <span class="k">else</span><span class="p">:</span>          <span class="n">dy</span> <span class="o">=</span> <span class="mf">0</span>

        <span class="c"># Translate and move window.</span>
        <span class="n">pos</span><span class="o">.</span><span class="n">translate</span><span class="p">(</span><span class="n">dx</span><span class="p">,</span> <span class="n">dy</span><span class="p">)</span>
        <span class="n">window</span><span class="o">.</span><span class="n">set_position</span><span class="p">(</span><span class="n">pos</span><span class="p">)</span>

<span class="n">grammar</span><span class="o">.</span><span class="n">add_rule</span><span class="p">(</span><span class="n">TranslateRule</span><span class="p">())</span>


<span class="c">#---------------------------------------------------------------------------</span>

<span class="k">class</span> <span class="nc">ResizeRule</span><span class="p">(</span><span class="n">CompoundRule</span><span class="p">):</span>

    <span class="n">spec</span> <span class="o">=</span> <span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">resize_win</span>
    <span class="n">extras</span> <span class="o">=</span> <span class="p">[</span>
              <span class="n">win_selector</span><span class="p">,</span>                  <span class="c"># Window selector element</span>
              <span class="n">mon_selector</span><span class="p">,</span>                  <span class="c"># Monitor selector element</span>
              <span class="n">position</span><span class="p">,</span>                      <span class="c"># Position element</span>
             <span class="p">]</span>

    <span class="k">def</span> <span class="nf">_process_recognition</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">node</span><span class="p">,</span> <span class="n">extras</span><span class="p">):</span>
        <span class="c"># Determine which window to place on which monitor.</span>
        <span class="n">window</span> <span class="o">=</span> <span class="n">extras</span><span class="p">[</span><span class="s">&quot;win_selector&quot;</span><span class="p">]</span>
        <span class="n">pos</span> <span class="o">=</span> <span class="n">window</span><span class="o">.</span><span class="n">get_position</span><span class="p">()</span>
        <span class="n">monitor</span> <span class="o">=</span> <span class="n">window</span><span class="o">.</span><span class="n">get_containing_monitor</span><span class="p">()</span><span class="o">.</span><span class="n">rectangle</span>

        <span class="c"># Determine horizontal positioning.</span>
        <span class="n">nodes</span> <span class="o">=</span> <span class="n">node</span><span class="o">.</span><span class="n">get_children_by_name</span><span class="p">(</span><span class="s">&quot;horz&quot;</span><span class="p">)</span>
        <span class="n">horizontals</span> <span class="o">=</span> <span class="p">[(</span><span class="n">monitor</span><span class="o">.</span><span class="n">x1</span> <span class="o">+</span> <span class="n">n</span><span class="o">.</span><span class="n">value</span><span class="p">()</span> <span class="o">*</span> <span class="n">monitor</span><span class="o">.</span><span class="n">dx</span><span class="p">)</span> <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">nodes</span><span class="p">]</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">horizontals</span><span class="p">)</span> <span class="o">==</span> <span class="mf">1</span><span class="p">:</span>
            <span class="n">horizontals</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="n">pos</span><span class="o">.</span><span class="n">x1</span><span class="p">,</span> <span class="n">pos</span><span class="o">.</span><span class="n">x2</span><span class="p">])</span>
        <span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">horizontals</span><span class="p">)</span> <span class="o">!=</span> <span class="mf">2</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_log</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%s</span><span class="s">: Internal error.&quot;</span>  <span class="o">%</span> <span class="bp">self</span><span class="p">)</span>
            <span class="k">return</span>
        <span class="n">x1</span><span class="p">,</span> <span class="n">x2</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">horizontals</span><span class="p">),</span> <span class="nb">max</span><span class="p">(</span><span class="n">horizontals</span><span class="p">)</span>

        <span class="c"># Determine vertical positioning.</span>
        <span class="n">nodes</span> <span class="o">=</span> <span class="n">node</span><span class="o">.</span><span class="n">get_children_by_name</span><span class="p">(</span><span class="s">&quot;vert&quot;</span><span class="p">)</span>
        <span class="n">verticals</span> <span class="o">=</span> <span class="p">[(</span><span class="n">monitor</span><span class="o">.</span><span class="n">y1</span> <span class="o">+</span> <span class="n">n</span><span class="o">.</span><span class="n">value</span><span class="p">()</span> <span class="o">*</span> <span class="n">monitor</span><span class="o">.</span><span class="n">dy</span><span class="p">)</span> <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">nodes</span><span class="p">]</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">verticals</span><span class="p">)</span> <span class="o">==</span> <span class="mf">1</span><span class="p">:</span>
            <span class="n">verticals</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="n">pos</span><span class="o">.</span><span class="n">y1</span><span class="p">,</span> <span class="n">pos</span><span class="o">.</span><span class="n">y2</span><span class="p">])</span>
        <span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">verticals</span><span class="p">)</span> <span class="o">!=</span> <span class="mf">2</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_log</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%s</span><span class="s">: Internal error.&quot;</span>  <span class="o">%</span> <span class="bp">self</span><span class="p">)</span>
            <span class="k">return</span>
        <span class="n">y1</span><span class="p">,</span> <span class="n">y2</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">verticals</span><span class="p">),</span> <span class="nb">max</span><span class="p">(</span><span class="n">verticals</span><span class="p">)</span>

        <span class="c"># Move window.</span>
        <span class="n">pos</span> <span class="o">=</span> <span class="n">Rectangle</span><span class="p">(</span><span class="n">x1</span><span class="p">,</span> <span class="n">y1</span><span class="p">,</span> <span class="n">x2</span><span class="o">-</span><span class="n">x1</span><span class="p">,</span> <span class="n">y2</span><span class="o">-</span><span class="n">y1</span><span class="p">)</span>
        <span class="n">window</span><span class="o">.</span><span class="n">set_position</span><span class="p">(</span><span class="n">pos</span><span class="p">)</span>

<span class="n">grammar</span><span class="o">.</span><span class="n">add_rule</span><span class="p">(</span><span class="n">ResizeRule</span><span class="p">())</span>


<span class="c">#---------------------------------------------------------------------------</span>

<span class="k">class</span> <span class="nc">StretchRule</span><span class="p">(</span><span class="n">CompoundRule</span><span class="p">):</span>

    <span class="n">spec</span> <span class="o">=</span> <span class="n">config</span><span class="o">.</span><span class="n">lang</span><span class="o">.</span><span class="n">stretch_win</span>
    <span class="n">extras</span> <span class="o">=</span> <span class="p">[</span>
              <span class="n">win_selector</span><span class="p">,</span>                  <span class="c"># Window selector element</span>
              <span class="n">position</span><span class="p">,</span>                      <span class="c"># Position element</span>
             <span class="p">]</span>

    <span class="k">def</span> <span class="nf">_process_recognition</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">node</span><span class="p">,</span> <span class="n">extras</span><span class="p">):</span>
        <span class="c"># Determine which window to place.</span>
        <span class="n">window</span> <span class="o">=</span> <span class="n">extras</span><span class="p">[</span><span class="s">&quot;win_selector&quot;</span><span class="p">]</span>
        <span class="n">pos</span> <span class="o">=</span> <span class="n">window</span><span class="o">.</span><span class="n">get_position</span><span class="p">()</span>
        <span class="n">monitor</span> <span class="o">=</span> <span class="n">window</span><span class="o">.</span><span class="n">get_containing_monitor</span><span class="p">()</span><span class="o">.</span><span class="n">rectangle</span>

        <span class="c"># Determine horizontal positioning.</span>
        <span class="n">horizontals</span> <span class="o">=</span> <span class="p">[</span><span class="n">pos</span><span class="o">.</span><span class="n">x1</span><span class="p">,</span> <span class="n">pos</span><span class="o">.</span><span class="n">x2</span><span class="p">]</span>
        <span class="n">child</span> <span class="o">=</span> <span class="n">node</span><span class="o">.</span><span class="n">get_child_by_name</span><span class="p">(</span><span class="s">&quot;horz&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">child</span><span class="p">:</span> <span class="n">horizontals</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">monitor</span><span class="o">.</span><span class="n">x1</span> <span class="o">+</span> <span class="n">child</span><span class="o">.</span><span class="n">value</span><span class="p">()</span> <span class="o">*</span> <span class="n">monitor</span><span class="o">.</span><span class="n">dx</span><span class="p">)</span>
        <span class="n">x1</span><span class="p">,</span> <span class="n">x2</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">horizontals</span><span class="p">),</span> <span class="nb">max</span><span class="p">(</span><span class="n">horizontals</span><span class="p">)</span>

        <span class="c"># Determine vertical positioning.</span>
        <span class="n">verticals</span> <span class="o">=</span> <span class="p">[</span><span class="n">pos</span><span class="o">.</span><span class="n">y1</span><span class="p">,</span> <span class="n">pos</span><span class="o">.</span><span class="n">y2</span><span class="p">]</span>
        <span class="n">child</span> <span class="o">=</span> <span class="n">node</span><span class="o">.</span><span class="n">get_child_by_name</span><span class="p">(</span><span class="s">&quot;vert&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">child</span><span class="p">:</span> <span class="n">verticals</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">monitor</span><span class="o">.</span><span class="n">y1</span> <span class="o">+</span> <span class="n">child</span><span class="o">.</span><span class="n">value</span><span class="p">()</span> <span class="o">*</span> <span class="n">monitor</span><span class="o">.</span><span class="n">dy</span><span class="p">)</span>
        <span class="n">y1</span><span class="p">,</span> <span class="n">y2</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">verticals</span><span class="p">),</span> <span class="nb">max</span><span class="p">(</span><span class="n">verticals</span><span class="p">)</span>

        <span class="c"># Move window.</span>
        <span class="n">pos</span> <span class="o">=</span> <span class="n">Rectangle</span><span class="p">(</span><span class="n">x1</span><span class="p">,</span> <span class="n">y1</span><span class="p">,</span> <span class="n">x2</span><span class="o">-</span><span class="n">x1</span><span class="p">,</span> <span class="n">y2</span><span class="o">-</span><span class="n">y1</span><span class="p">)</span>
        <span class="n">window</span><span class="o">.</span><span class="n">set_position</span><span class="p">(</span><span class="n">pos</span><span class="p">)</span>

<span class="n">grammar</span><span class="o">.</span><span class="n">add_rule</span><span class="p">(</span><span class="n">StretchRule</span><span class="p">())</span>




<span class="n">grammar</span><span class="o">.</span><span class="n">load</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">unload</span><span class="p">():</span>
    <span class="k">global</span> <span class="n">grammar</span>
    <span class="k">if</span> <span class="n">grammar</span><span class="p">:</span> <span class="n">grammar</span><span class="o">.</span><span class="n">unload</span><span class="p">()</span>
    <span class="n">grammar</span> <span class="o">=</span> <span class="bp">None</span>
</pre></div>
</td></tr></table></div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="index.html">Table Of Contents</a></h3>
            <ul>
<li><a class="reference external" href="">Command-module for moving and controlling <strong>windows</strong></a><ul>
<li><a class="reference external" href="#commands">Commands</a></li>
<li><a class="reference external" href="#usage-examples">Usage examples</a></li>
<li><a class="reference external" href="#configuration-examples">Configuration examples</a><ul>
<li><a class="reference external" href="#configuration-winctrl-en-txt">Configuration &#8220;_winctrl-en.txt&#8221;</a></li>
</ul>
</li>
<li><a class="reference external" href="#module-source-code">Module source code</a></li>
</ul>
</li>
</ul>

            <h4>Previous topic</h4>
            <p class="topless"><a href="mod-_dragonfly_tools.html" title="previous chapter">Command-module for managing <strong>Dragonfly</strong></a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="mod-_multiedit.html" title="next chapter">Command-module for cursor movement and <strong>editing</strong></a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="_sources/mod-_winctrl.txt">Show Source</a></li>
            </ul>
          <h3>Quick search</h3>
            <form class="search" action="search.html" method="get">
              <input type="text" name="q" size="18" /> <input type="submit" value="Go" />
              <input type="hidden" name="check_keywords" value="yes" />
              <input type="hidden" name="area" value="default" />
            </form>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="mod-_multiedit.html" title="Command-module for cursor movement and editing"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="mod-_dragonfly_tools.html" title="Command-module for managing Dragonfly"
             accesskey="P">previous</a> |</li>
        <li><a href="index.html">Dragonfly-modules v0.1 documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; Copyright 2008, Christo Butcher.
      Last updated on Apr 10, 2009.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.5.1.
    </div>
  </body>
</html>